//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;

namespace OpenLS.Chart
{
    public abstract class DrawingAnchor : IOfficeXmlSerializable
    {
        private IAnchored _content;

        public IAnchored Content
        {
            get { return _content; }
            set { _content = value; }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            ReadXmlCore(context);
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            WriteXmlCore(context);
        }

        #endregion

        public static DrawingAnchor ReadAnchor(ReadContext context)
        {
            switch (context.ReaderLocalName)
            {
                case "relSizeAnchor":
                    return context.ReadElement<DrawingRelativeAnchor>();
                case "absSizeAnchor":
                    return context.ReadElement<DrawingAbsoluteSizeAnchor>();
                case null:
                    return null;
                default:
                    throw new NotSupportedException();
            }
        }

        internal void ReadXmlBase(ReadContext context)
        {
            var shapeReader = context.GetData<IShapeReader>();
            if (shapeReader == null)
                shapeReader = new ShapeHelper();
            Content = shapeReader.ReadContent(context);
        }

        internal void WriteXmlBase(WriteContext context)
        {
            context.WriteElement(Content);
        }

        internal abstract void ReadXmlCore(ReadContext context);

        internal abstract void WriteXmlCore(WriteContext context);
    }
}